<!--
 * @Author: shy_didi
 * @Date: 2020-06-09 10:02:20
 * @LastEditTime: 2020-06-09 17:58:56
 * @LastEditors: Please set LastEditors
 * @Description: 新闻中心
 * @FilePath: \发明人\fmr\src\pages\home\components\news.vue
--> 
<template>
  <div class="news">
    <p class="eng">NEWS</p>
    <p class="chi">新闻中心</p>
    <!-- 新闻通知 -->
    <h1>新闻通知</h1>
    <van-swipe class="my-swipe" :autoplay="6000" indicator-color="white">
      <van-swipe-item class="my-swipe-item" v-for="(items,idx) of news" :key="idx">
        <div class="item-content" v-for="item of items" :key="item.id">
          <div class="calendar">
            <h2 class="date">{{item.day}}</h2>
            <h2 class="ym">{{item.date_ym}}</h2>
          </div>
          <div class="info">
            <h2 class="title">{{item.title}}</h2>
            <div class="content">{{item.content}}</div>
          </div>
        </div>
      </van-swipe-item>
    </van-swipe>

    <!-- 通知公告 -->
    <h1>通知公告</h1>
    <van-swipe class="my-swipe" :autoplay="6000" indicator-color="white">
      <van-swipe-item class="my-swipe-item" v-for="(items,idx) of notices" :key="idx">
        <div class="item-content" v-for="item of items" :key="item.id">
          <div class="calendar">
            <h2 class="date">{{item.day}}</h2>
            <h2 class="ym">{{item.date_ym}}</h2>
          </div>
          <div class="info">
            <h2 class="title">{{item.title}}</h2>
            <div class="content">{{item.content}}</div>
          </div>
        </div>
      </van-swipe-item>
    </van-swipe>
    <div class="more">
      <van-button class="button"  to="/news" plain>更多新闻</van-button>
    </div>
  </div>
</template>

<script>
import * as api from "@/request/api/apiList/document.js";
export default {
  data() {
    return {
      news: [], //新闻通知
      per_num: 2, //每个轮播显示的新闻条数
      notices: [] //通知公告
    };
  },
  methods: {
    async API_document(type, is_top, per_num, page = 1, perPage = 10) {
      // 获取文章列表数据
      let params = {
        type,
        is_top,
        per_num,
        page,
        perPage
      };
      return await api.document(params);
    },
    //
    filter(arr) {
      let newArr = []; //存放arr数组遍历完成的新数组
      let per_num = this.per_num;
      for (let i = 0; i < arr.length; i += per_num) {
        newArr.push(arr.slice(i, i + per_num));
      }
      return newArr;
    }
  },
  async created() {
    let res = await this.API_document(1, 1, 2);
    //console.log(res);
    if (res.data.code === 200) {
      this.news = this.filter(res.data.data);
    }

    let res2 = await this.API_document(2, 1, 2);
    //console.log(res2);
    this.notices = this.filter(res2.data.data);
  }
};
</script>

<style lang="scss" scoped>
@import "@/style/home.scss";
</style>